/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.asportoonline.various;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author fe
 */
public class UtenteValidation {
    
    public static boolean regexpr(String patt, String arg) {
        Pattern p = Pattern.compile(patt);
        Matcher m = p.matcher(arg);
        return m.matches();
    }
    
    public static boolean validateEmail(String mail) {
        return regexpr("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$", mail);
    }
    
    public static boolean validatePhoneNumber(String phone) {
        return regexpr("^[0-9]+$", phone);
    }
    
    //tra 6 e 20 caratteri. La prima deve essere una lettera, poi lettere, numeri e _; infine l-ultima non puo essere _    
    public static boolean validatePassword(String pwd) {
        return regexpr("^[a-zA-Z0-9][a-zA-Z0-9_]{4,19}[a-zA-Z0-9]$", pwd);
    }
    
    public static boolean validateCF(String cf) {
        return regexpr("^[a-zA-Z]{6}[0-9]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9]{2}([a-zA-Z]{1}[0-9]{3})[a-zA-Z]{1}$", cf);
    }
    
    public static boolean validateName(String name) {
        return regexpr("^[a-zA-Z]+([ ][a-zA-Z]+)?$", name);
    }

    public static boolean validateSurname(String surname) {
        return regexpr("^[a-zA-Z]+([ ][a-zA-Z]+)?$", surname);
    }

    public static boolean validateDate(String date){
 
        Pattern pattern = Pattern.compile("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)");
        Matcher matcher = pattern.matcher(date);
 
        if(matcher.matches()){
 
            matcher.reset();

            if(matcher.find()){

                String day = matcher.group(1);
                String month = matcher.group(2);
                int year = Integer.parseInt(matcher.group(3));

                if (day.equals("31") && 
                    (month.equals("4") || month .equals("6") || month.equals("9") ||
                    month.equals("11") || month.equals("04") || month .equals("06") ||
                    month.equals("09"))) {

                        return false; // only 1,3,5,7,8,10,12 has 31 days

                } 
                else if (month.equals("2") || month.equals("02")) {
                    //leap year
                    if(year % 4==0){
                          if(day.equals("30") || day.equals("31")){
                                  return false;
                          }
                          else {
                                  return true;
                          }
                    }
                    else {
                        if(day.equals("29")||day.equals("30")||day.equals("31")){
                            return false;
                        } 
                        else {
                            return true;
                        }
                    }
                }
                else {				 
                    return true;				 
                }
            }
            else {
                return false;
	    }		  
        }
        else {
	  return false;
        }			    
    }
}